package whf.framework.mail.web;

import whf.framework.entity.AbstractEntity;
import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.mail.entity.Mail;
import whf.framework.mail.service.MailService;
import whf.framework.mail.service.MailServiceImp;
import whf.framework.web.struts.WebData;
import whf.framework.web.struts.WebDispatchAction;
import whf.framework.web.struts.WebForward;

/**
 * @author wanghaifeng
 * @email king@126.com
 * @modify 2006-05-31
 */
public class MailAction extends WebDispatchAction {
	private static Log log = LogFactory.getLog(MailAction.class);
	
	@Override
	public WebForward create(AbstractEntity webEntity, WebData data) throws Exception {
		WebForward result = super.create(webEntity, data);
		Mail mail = (Mail)webEntity;
		if(mail.isHasSent()) {
			try{
				MailService service = MailServiceImp.getMailService();
				service.send(mail);
			} catch(Exception e){
				log.error(this, e);
			}
		}
		return result;
	}

	public WebForward send(AbstractEntity webEntity, WebData data) throws Exception {
		long[] ids = data.getLongParameters("id", 0);
		MailService service = MailServiceImp.getMailService();
		for(long id: ids){
			if(id == 0) continue;
			try{
				Mail mail = service.findByPrimaryKey(id);
				service.send(mail);
			} catch(Exception e){
				log.error(this, e);
			}
		}
		return super.list(webEntity, data);
	}
} 